<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-menu default-active="1" class="el-menu-demo" mode="horizontal">
          <el-menu-item index="1">公司设置</el-menu-item>
        </el-menu>
        <el-tabs v-model="activeName" style="margin-top: 30px; padding: 0 20px">
          <el-tab-pane label="角色管理" name="first">
            <!-- 新增角色按钮 -->
            <el-row style="margin:10px 0 20px">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="showDialog = true">新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border :data="list">
              <el-table-column align="center" label="序号" width="120" type="index"></el-table-column>
              <el-table-column align="center" label="角色名称" width="240" prop="name"></el-table-column>
              <el-table-column align="center" label="描述" prop="description"></el-table-column>
              <el-table-column align="center" label="操作">
                <template slot-scope="scope">
                  <el-button size="mini" type="success" @click="assignPerm(scope.row.id)">分配权限</el-button>
                  <el-button size="mini" type="primary" @click="getRoleDetail(scope.row.id)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="delRole(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height: 60px; margin-top: 10px;">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                layout="total, prev, pager, next"
                :total="total"
                @current-change="handleCurrentChange"
              ></el-pagination>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" type="info" show-icon :closable="false" style="font-size: 12px"></el-alert>
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="企业名称">
                <el-input v-model="formData.name" disabled style="width: 400px;"></el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width: 400px;"></el-input>
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input v-model="formData.companyPhone" disabled style="width: 400px;"></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width: 400px;"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width: 400px;"></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog title="编辑弹层" :visible.sync="showDialog" @close="btnCancel">
      <el-form ref="editFormRef" :model="editForm" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="editForm.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
      <el-tree
        ref="permTree"
        :data="permData"
        :props="defaultProps"
        default-expand-all
        show-checkbox
        :check-strictly="true"
        :default-checked-keys="selectCheck"
        node-key="id"
      ></el-tree>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnPermOk">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getRoleList, getCompanyList, delRole, getRoleDetail, editRole, addRole, assignPerm } from '@/api/setting.js'
import { getPermissionList } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      activeName: 'first',
      list: [],
      page: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      // 公司信息
      formData: {},
      showDialog: false,
      editForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      showPermDialog: false, // 分配权限弹出层
      permData: [], // 分配权限数据
      defaultProps: {
        label: 'name'
      },
      roleId: null, // 用来记录分配角色的id
      selectCheck: [] // 定义一个数组来接收 已经选中的节点
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyList()
  },
  methods: {
    async getRoleList() {
      const results = await getRoleList(this.page)
      this.list = results.rows
      this.total = results.total
      // console.log(results)
    },
    handleCurrentChange(val) {
      this.page.page = val
      this.getRoleList()
    },
    async getCompanyList() {
      this.formData = await getCompanyList(this.companyId)
      // console.log(this.formData)
    },
    async delRole(id) {
      // console.log(id)
      /* this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return delRole(id)
      }).then(() => {
        this.$message.success('删除此角色成功！')
        this.getRoleList()
      }).catch(() => {
        this.$message.info('已取消删除！')
      }) */
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delRole(id)
        this.getRoleList()
        this.$message.success('删除此角色成功！')
      } catch (error) {
        console.log(error)
      }
    },
    async getRoleDetail(id) {
      this.editForm = await getRoleDetail(id)
      this.showDialog = true
    },
    async btnOk() {
      try {
        await this.$refs.editFormRef.validate()
        // 只有校验通过的情况下 才会执行await的下方内容
        // roleForm这个对象有id就是编辑 没有id就是新增
        if (this.editForm.id) {
          // 编辑
          await editRole(this.editForm)
        } else {
          // 新增
          await addRole(this.editForm)
        }
        this.getRoleList()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      this.editForm = {
        name: '',
        description: ''
      }
      this.$refs.editFormRef.resetFields()
      this.showDialog = false
    },
    async assignPerm(id) {
      this.permData = transListToTreeData(await getPermissionList(), '0')
      // 应该去获取 这个id的 权限点
      // 有id 就可以 id应该先记录下来
      this.roleId = id
      const { permIds } = await getRoleDetail(id) // permIds是当前角色所拥有的权限点数据
      console.log(permIds)
      this.selectCheck = permIds // 将当前角色所拥有的权限id赋值
      this.showPermDialog = true
    },
    async btnPermOk() {
      await assignPerm({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },
    btnPermCancel() {
      this.selectCheck = []
      this.showPermDialog = false
    }
  }
}
</script>

<style>
.el-menu--horizontal > .el-menu-item.is-active {
  color: #409eff;
}
.el-menu--horizontal .el-menu-item:not(.is-disabled):focus,
.el-menu--horizontal .el-menu-item:not(.is-disabled):hover {
  color: #409eff;
  cursor: default;
}
.el-alert__title {
  font-size: 12px;
}
textarea {
  resize: none !important;
}
</style>
